﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tosi
{
    class Evaluation
    {

        private GeneticAlgorithm genAlg;
        private Feeder feeder;
        private Statistics st;
        private int index;

        /*private void updateLightPriorities()
        {
            for (int i = 0; i < Var.StrNumber; i++)
            {
                Var.STREETS[i].myLight.setStationaryCars();
                Var.STREETS[i].myLight.setStreetLoad();
                Var.STREETS[i].myLight.incTime();
                Var.STREETS[i].myLight.initPriority();
            }
        }
        private void calcLightPriorities()
        {
            for (int i = 0; i < Var.StrNumber; i++)
            {
                Var.STREETS[i].getMyLight().initPriority();
            }
        }
        private void setAllLights()
        {
            for (int i = 0; i < Var.StrNumber; i++)
            {
                Var.STREETS[i].getMyLight().setLight();
            }
        }
        private void limitPriorities()
        {
            for (int i = 0; i < Var.JncNumber; i++)
            {
                Var.JUNCTIONS[i].setLights(i);
            }
            //Console.ReadKey();
        }*/
        private void setAllLights()
        {
            for (int i = 0; i < Var.JncNumber; i++)
            {
                Var.JUNCTIONS[i].setLights();
            }
            //Console.ReadKey();
        }

        public Evaluation(GeneticAlgorithm parent, int ind)
        {
            Var.gr.changeToFront();
            st = new Statistics();
            genAlg = parent;
            feeder = new Feeder(3, st, genAlg);
            index = ind;
        }
        public void Start()
        {
            int xx = 0;
            feeder.rotateTables();
            for (int interval = 0; interval < Var.Intervals; interval++)
            {
                for (int iterations = 0; iterations < Var.Iterations; iterations++)
                {
                    for (int j = 0; j < Var.StrNumber; j++)
                    {
                        for (int k = 0; k < Var.STREETS[j].cars.Count; k++)
                        {
                            Var.STREETS[j].cars[k].moveCar();
                        }
                    }
                    Var.gr.clearFront();
                    Var.gr.cars();
                    Var.excelWriter.writeIteration(xx++);
                    System.Threading.Thread.Sleep(Var.Delay);

                    //updateLightPriorities();
                    setAllLights();
                    //limitPriorities();
                    st.startIterationStat(iterations);    //stat*/
                }
                st.startIntervalStat();    //stat
                feeder.rotateTables();
                genAlg.intervalFitness(index);
                //DataSaver.dfh
            }
            feeder.rotateTables();
            //genAlg.intervalFitness(index);
            genAlg.Fitness(index);
            //DataSaver.dfh
            feeder.rotateTables();
            //DataSaver.dfh
        }

    }

}